package smartcrud.spring.controllers.rbac;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import smartcrud.common.orm.Page;
import smartcrud.common.orm.PropertyFilter;
import smartcrud.common.orm.jdbc.JdbcUtils;
import smartcrud.spring.controllers.rbac.editor.MenuEditor;
import smartcrud.spring.models.rbac.Menu;
import smartcrud.spring.models.rbac.MenuService;
import smartcrud.spring.models.rbac.Role;
import smartcrud.spring.models.rbac.RoleService;

/**
 * Rails 1.2中对REST的实现默认把controller中的方法限制在7个：<br>
 * index、show、new、edit、create、 update和destroy.<br>
 * 而根据SpringSide4,使用Restful风格的Urls:<br>
 * 
 * <pre>
 * List page     : GET  /role/ 
 * Show page     : GET  /role/{id}
 * Create page   : GET  /role/create
 * Create action : POST /role/create
 * Update page   : GET  /role/update/{id}
 * Update action : POST /role/update
 * Delete action : GET  /role/delete
 * </pre>
 * 
 * @author cyper.yin
 * 
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {
	private static final String SUCCESS = "redirect:/role/";

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "")
	public String index(
			@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
			Model model, 
			HttpServletRequest request) {
		
		List<PropertyFilter> filters = JdbcUtils.buildPropertyFilters(request);
		
		Page<Role> page = new Page<Role>(10);
		page.setPageIndex(pageIndex);
		page = roleService.findByPage(page,filters);
		
		model.addAttribute("page", page);
		
		return "role/role_index";
	}

	@RequestMapping("/{id}")
	public ModelAndView show(@PathVariable("id") Long id) {
		Role role = roleService.findRoleById(id);
		ModelAndView mv = new ModelAndView("role/role_show");
		mv.addObject(role);

		return mv;
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(ModelMap model) {
		Role role = new Role();
		model.addAttribute(role);
		return "role/role_new";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@ModelAttribute("role") Role role,
			RedirectAttributes redirectAttributes) {
		roleService.createRole(role);
		redirectAttributes.addFlashAttribute("message", "create success!");

		return SUCCESS;
	}
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) throws ServletException {
		//following line doesn't work for Invalid property 'allMenus' of bean class [smartcrud.spring.models.rbac.Role]
		//so I decided not to use the tag: sf:select path="allMenus"
		//binder.setDisallowedFields("allMenus");
		binder.registerCustomEditor(Menu.class, new MenuEditor(menuService));
	}
	
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(Model model, @PathVariable("id") Long id) {
		model.addAttribute("allMenus",menuService.findAllMenus());
		
		Role role = roleService.findRoleById(id);
		model.addAttribute("role",role);
		
		//FIXME 为什么页面取不到role中的menus, 还要显示指定?
		model.addAttribute("menus",role.getMenus());
		return "role/role_edit";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@ModelAttribute("role") Role role,
			RedirectAttributes redirectAttributes) {
		roleService.updateRole(role);
		redirectAttributes.addFlashAttribute("message", "update success!");
		return SUCCESS;
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		roleService.deleteRole(id);
		redirectAttributes.addFlashAttribute("message", "delete success!");
		return SUCCESS;
	}
}
